package com.lianxueda.drawing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.domain.dto.WxUsersFromDTO;
import com.lianxueda.drawing.domain.dto.WxUsersQueryDTO;
import com.lianxueda.drawing.domain.pojo.WxUser;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.service.WxUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 29264
 */
@RestController
@RequestMapping("/api/v1/wxUser")
@Tag(name = "微信用户管理模块", description = "微信用户管理模块接口")
@RequiredArgsConstructor

public class WxUserController {

    @Resource
    private WxUserService wxUserService;



    @Operation(summary = "分页查询用户列表")
    @PostMapping("/list")
    public R list(@RequestBody WxUsersQueryDTO wxUsersQueryDTO) {
        //使用Lambda表达式【指定需要查询的字段】
        IPage<WxUser> page = new Page<>(wxUsersQueryDTO.getCurPage(),
                wxUsersQueryDTO.getPageSize());
        //2.设置查询条件
        QueryWrapper<WxUser> queryWrapper = new QueryWrapper<>();
        //用户姓名
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        wxUsersQueryDTO.getName()),
                WxUser::getName,
                wxUsersQueryDTO.getName());

        //手机号
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        wxUsersQueryDTO.getPhoneNumber()),
                WxUser::getPhoneNumber,
                wxUsersQueryDTO.getPhoneNumber());

        //用户昵称
        queryWrapper.lambda().like(StringUtils.isNotBlank(
                        wxUsersQueryDTO.getNickname()),
                WxUser::getNickname,
                wxUsersQueryDTO.getNickname());



        //查询指定字段
        //queryWrapper.select("id", "title");
        //排除字段查询
        queryWrapper.select(WxUser.class, info -> !info.getColumn().equals("deleted"));
        //3.查询
        IPage<WxUser> pageResult = wxUserService.page(page, queryWrapper);


        //4.封装，返回
        PageVO pageVo = new PageVO<>();

        pageVo.setTotal(pageResult.getTotal());
        pageVo.setList(pageResult.getRecords());

        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());


        return R.suc(pageVo);
    }
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "根据id删除用户")
    public R deleteById(@PathVariable Integer id){
        WxUser user = wxUserService.getById(id);
        if (user == null) {
            return R.fail("用户不存在");

        }
        user.setDeleted(0);
        wxUserService.removeById(id);
        return R.suc("删除成功");
    }
    @PostMapping("/add")
    @Operation(summary = "新增用户")
    public R add(@RequestBody WxUsersFromDTO wxUsersFromDTO) {
        WxUser users = new WxUser();
        users.setName(wxUsersFromDTO.getName());
        users.setPhoneNumber(wxUsersFromDTO.getPhoneNumber());
        users.setNickname(wxUsersFromDTO.getNickname());
        users.setAvatar(wxUsersFromDTO.getAvatar());
        users.setRelation(wxUsersFromDTO.getRelation());
        users.setCreateTime(LocalDateTime.now());
        users.setStudentId(wxUsersFromDTO.getStudentId());
        wxUserService.save(users);


        return R.suc("新增成功");
    }
    @PostMapping("/update")
    @Operation(summary = "修改用户")
    public R update(@RequestBody WxUsersFromDTO wxUsersFromDTO) {
        WxUser users = new WxUser();
        users.setId(wxUsersFromDTO.getId());
        users.setName(wxUsersFromDTO.getName());
        users.setPhoneNumber(wxUsersFromDTO.getPhoneNumber());
        users.setNickname(wxUsersFromDTO.getNickname());
        users.setAvatar(wxUsersFromDTO.getAvatar());
        users.setRelation(wxUsersFromDTO.getRelation());
        users.setStudentId(0);
        wxUserService.updateById(users);

        return R.suc("修改成功");
    }

    @PostMapping("/getParentOpenId")
    @Operation(summary = "根据学生的id查询父母的openid")
    public R getParentOpenId(@RequestBody List<Integer> studentId) {
        List<Map<Integer, String>> openIdList = wxUserService.getOpenIdByStudentIds(studentId);

        if (!openIdList.isEmpty()) {
            return R.suc(openIdList);
        } else {
            return R.err("未找到该家长");
        }
    }


}
